package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.dto.PostDTO;
import com.example.mybatisplusdemo.model.vo.MerchantPostVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IMerchantPostService;
import com.example.mybatisplusdemo.model.domain.MerchantPost;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-04
 * @version v1.0
 */
@RestController
@RequestMapping("/api/merchantPost")
public class MerchantPostController {

    private final Logger logger = LoggerFactory.getLogger( MerchantPostController.class );

    @Autowired
    private IMerchantPostService merchantPostService;

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        MerchantPost merchantPost = merchantPostService.getById(id);
        return JsonResponse.success(merchantPost);
    }

    // 新增店铺推文
    @PostMapping("insertANewPost")
    public JsonResponse insertANewPost(@RequestBody MerchantPost merchantPost) {
        boolean isInserted = merchantPostService.save(merchantPost);
        return JsonResponse.success(isInserted);
    }

    // 获取公共推文
    @GetMapping("listPublicPost")
    public JsonResponse listPublicPost(PageDTO pageDTO) {
        Page<PostDTO> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        List<PostDTO> postList = merchantPostService.listPublicPost(page);
        return JsonResponse.success(postList);
    }

    //根据用户id查询推文
    @GetMapping("listFavoritePostByUserId")
    public JsonResponse listFavoritePostByUserId(PageDTO pageDTO, Long userId) {
        Page<PostDTO> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        List<PostDTO> postList = merchantPostService.listFavoritePostByUserId(page, userId);
        return JsonResponse.success(postList);
    }

    // 根据店铺ID分页查询推文
    @GetMapping("listPagingPostsByStoreIdWithStoreName")
    public JsonResponse listPagingPostsByStoreIdWithStoreName(PageDTO pageDTO, Long storeId) {
        Page<MerchantPostVO> page = merchantPostService.listPagingPostsByStoreIdWithStoreName(pageDTO, storeId);
        return JsonResponse.success(page);
    }

    //删除推文
    @GetMapping("deletePostById")
    public JsonResponse deletePostById(Long id) {
        boolean isDeleted = merchantPostService.removeById(id);
        return JsonResponse.success(isDeleted);
    }

    //更新推文
    @PostMapping("updatePost")
    public JsonResponse updatePost(@RequestBody MerchantPost updatedPost) {
        boolean b = merchantPostService.updateById(updatedPost);
        return  JsonResponse.success(b);
    }
}

